import { Options, Vue } from 'vue-class-component'
import navigator from '@/components/common/navigator.vue'
import store from '@/store'
import { ElMessage } from 'element-plus'
import router from '@/router'

interface IShoppingMallParams {
  commodityId: string
}

@Options({
  components: {
    navigator
  }
})

export default class reportMall extends Vue {
  goodsDetail: any = null
  input1: string = null
  tempPriceHashmap: { [key: number]: any[] } = {}
  temTimeHashmap: { [key: number]: any[] } = {}
  finalGoodsArray: any[] = []
  goodsArr: any[] = []
  searchGoodsArr: any[] = []
  selectArr: any[] = []
  pageSum = 1
  curPage = 1
  eachPage = 8
  tagArr = [
    {
      id: 0,
      tagName: '100元以下',
      tagStatus: false
    }, {
      id: 1,
      tagName: '100-500元',
      tagStatus: false
    }, {
      id: 2,
      tagName: '500-1000元',
      tagStatus: false
    }, {
      id: 3,
      tagName: '1000-5000元',
      tagStatus: false
    }, {
      id: 4,
      tagName: '5000元以上',
      tagStatus: false
    }
  ]

  timeArr = [
    {
      id: 0,
      date: '2016年之前',
      timeStatus: false
    }, {
      id: 1,
      date: '2016-2018年',
      timeStatus: false
    }, {
      id: 2,
      date: '2018-2020年',
      timeStatus: false
    }, {
      id: 3,
      date: '2020-2021年',
      timeStatus: false
    }, {
      id: 4,
      date: '2021年至今',
      timeStatus: false
    }
  ]

  shoppingMallParams: IShoppingMallParams = {
    commodityId: ''
  }

  onChange (e, tagChecked) {
    for (const tagArrElement of this.tagArr) {
      let index = 0
      const arr: any[] = []
      for (const timeArrElement of this.timeArr) {
        if (timeArrElement.timeStatus === true) {
          timeArrElement.timeStatus = false
          for (let i = 0; i < 5; i++) {
            this.tempPriceHashmap[i] = []
          }
          this.goodsArr = this.finalGoodsArray
        }
      }
      for (let i = 0; i < this.tagArr.length; i++) {
        if (this.tagArr[i].tagStatus === false) {
          index++
        }
      }
      if (index === 5 && this.goodsArr !== []) {
        this.goodsArr = this.finalGoodsArray
      }
      if (e === tagArrElement.id && tagChecked === false) {
        this.goodsArr = []
        tagChecked = true
        tagArrElement.tagStatus = tagChecked
        for (const finalGoodsArrayElement of this.finalGoodsArray) {
          if (finalGoodsArrayElement.commodityPrice < 100 && e === 0) {
            arr.push(finalGoodsArrayElement)
          }
          if ((finalGoodsArrayElement.commodityPrice >= 100 && finalGoodsArrayElement.commodityPrice < 500) && e === 1) {
            arr.push(finalGoodsArrayElement)
          }
          if ((finalGoodsArrayElement.commodityPrice >= 500 && finalGoodsArrayElement.commodityPrice < 1000) && e === 2) {
            arr.push(finalGoodsArrayElement)
          }
          if ((finalGoodsArrayElement.commodityPrice >= 1000 && finalGoodsArrayElement.commodityPrice < 5000) && e === 3) {
            arr.push(finalGoodsArrayElement)
          }
          if (finalGoodsArrayElement.commodityPrice >= 5000 && e === 4) {
            arr.push(finalGoodsArrayElement)
          }
        }
        this.tempPriceHashmap[e] = arr
        for (const hashmapKey in this.tempPriceHashmap) {
          for (const x of this.tempPriceHashmap[hashmapKey]) {
            this.goodsArr.push(x)
          }
        }
      } else if (e === tagArrElement.id && tagChecked === true) {
        this.goodsArr = []
        tagChecked = false
        tagArrElement.tagStatus = tagChecked
        this.tempPriceHashmap[e] = []
        for (const hashmapKey in this.tempPriceHashmap) {
          if (this.tempPriceHashmap[e] !== []) {
            for (const x of this.tempPriceHashmap[hashmapKey]) {
              this.goodsArr.push(x)
            }
          }
        }
      }
    }
    console.log(this.tempPriceHashmap)
  }

  onTimeChange (e, timeChecked) {
    for (const tagArrElement of this.timeArr) {
      let index = 0
      const arr: any[] = []
      for (const tagArrElement of this.tagArr) {
        if (tagArrElement.tagStatus === true) {
          tagArrElement.tagStatus = false
          for (let i = 0; i < 5; i++) {
            this.temTimeHashmap[i] = []
          }
          this.goodsArr = this.finalGoodsArray
        }
      }
      for (let i = 0; i < this.timeArr.length; i++) {
        if (this.timeArr[i].timeStatus === false) {
          index++
        }
      }
      if (index === 5 && this.goodsArr !== []) {
        this.goodsArr = this.finalGoodsArray
      }
      if (e === tagArrElement.id && timeChecked === false) {
        this.goodsArr = []
        timeChecked = true
        tagArrElement.timeStatus = timeChecked
        for (const finalGoodsArrayElement of this.finalGoodsArray) {
          if (Number(finalGoodsArrayElement.commodityUploadTime.substr(0, 4)) < 2016 && e === 0) {
            arr.push(finalGoodsArrayElement)
          }
          if (Number(finalGoodsArrayElement.commodityUploadTime.substr(0, 4)) >= 2016 && Number(finalGoodsArrayElement.commodityUploadTime.substr(0, 4)) < 2018 && e === 1) {
            arr.push(finalGoodsArrayElement)
          }
          if (Number(finalGoodsArrayElement.commodityUploadTime.substr(0, 4)) >= 2018 && Number(finalGoodsArrayElement.commodityUploadTime.substr(0, 4)) < 2020 && e === 2) {
            arr.push(finalGoodsArrayElement)
          }
          if (Number(finalGoodsArrayElement.commodityUploadTime.substr(0, 4)) >= 2020 && Number(finalGoodsArrayElement.commodityUploadTime.substr(0, 4)) < 2021 && e === 3) {
            arr.push(finalGoodsArrayElement)
          }
          if (Number(finalGoodsArrayElement.commodityUploadTime.substr(0, 4)) >= 2021 && e === 4) {
            arr.push(finalGoodsArrayElement)
          }
        }
        this.temTimeHashmap[e] = arr
        for (const hashmapKey in this.temTimeHashmap) {
          for (const x of this.temTimeHashmap[hashmapKey]) {
            this.goodsArr.push(x)
          }
        }
      } else if (e === tagArrElement.id && timeChecked === true) {
        this.goodsArr = []
        timeChecked = false
        tagArrElement.timeStatus = timeChecked
        this.temTimeHashmap[e] = []
        for (const hashmapKey in this.temTimeHashmap) {
          if (this.temTimeHashmap[e] !== []) {
            for (const x of this.temTimeHashmap[hashmapKey]) {
              this.goodsArr.push(x)
            }
          }
        }
      }
    }
    console.log(this.temTimeHashmap)
  }

  doSearch () {
    if (this.input1 === null) {
      this.searchGoodsArr = []
      ElMessage.warning({
        message: '搜索内容不能为空',
        type: 'warning'
      })
    } else {
      if (this.searchGoodsArr !== []) {
        this.searchGoodsArr = []
      }
      for (const goodsItem of this.finalGoodsArray) {
        if (goodsItem.commodityName.includes(this.input1)) {
          this.searchGoodsArr.push(goodsItem)
          this.goodsArr = this.searchGoodsArr
        }
      }
    }
  }

  created () {
    store.dispatch('displayAllgoods').then(response => {
      this.goodsArr = response.data.data
      // this.goodsArr.push()
      this.finalGoodsArray = response.data.data
      this.pageSum = Math.ceil(this.goodsArr.length / this.eachPage)
    })
  }

  handleSizeChange (val) {
    console.log(`每页 ${val} 条`)
    this.curPage = 1
    this.eachPage = val
  }

  // 当前页改变时触发 跳转其他页
  handleCurrentChange (val) {
    console.log(`当前页: ${val}`)
    this.curPage = val
  }

  // 查看商品详情
  checkDetail (e
    :
    string
  ) {
    this.shoppingMallParams.commodityId = e
    let code
    let goodsDetail
    let goodsName
    let goodsPrice
    let goodsDes
    let goodsUpTime
    let userName
    let userDes
    let userId
    store.dispatch('CheckGoodsDetail', this.shoppingMallParams).then(response => {
      console.log('connect success')
      goodsDetail = response.data.data
      this.goodsDetail = goodsDetail
      goodsName = this.goodsDetail.commodityName
      goodsPrice = this.goodsDetail.commodityPrice
      goodsDes = this.goodsDetail.commodityDescription
      goodsUpTime = this.goodsDetail.commodityUploadTime
      store.dispatch('CheckUserName', this.shoppingMallParams).then(response => {
        userName = response.data.data
        store.dispatch('CheckUserDes', this.shoppingMallParams).then(response => {
          userDes = response.data.data
          store.dispatch('UpdatePV', this.shoppingMallParams).then(response => {
            code = response.data.code
            store.dispatch('CheckUserId', this.shoppingMallParams).then(response => {
              console.log(this.shoppingMallParams.commodityId)
              userId = response.data.data
              console.log(userId)
              if (code === 200) {
                router.push({
                  path: '/goodsDetail',
                  query: {
                    name: goodsName,
                    price: goodsPrice,
                    des: goodsDes,
                    uptime: goodsUpTime,
                    username: userName,
                    userdes: userDes,
                    userid: userId,
                    goodsid: this.shoppingMallParams.commodityId
                  }
                })
              }
            }).catch(() => {
              console.log('connect failed')
            })
          }).catch(() => {
            console.log('connect failed')
          })
        }).catch(() => {
          console.log('connect failed')
        })
      }).catch(() => {
        console.log('connect failed')
      })
    }).catch(() => {
      console.log('connect failed')
    })
  }
}
